<template>
  <div class="login">
    <!-- 头部区域 -->
    <div class="header container">
      <a href="/index">
        <!-- <img src="/imgs/login-logo.png" alt="" /> -->
      </a>
    </div>
    <!-- 中间背景图片 -->
    <div class="login-bg">
      <img src="/imgs/login-bg.jpg" alt="" />
    </div>
    <!-- 登录框 -->
    <div class="center container">
      <div class="login-box">
        <div class="title">
          <span v-for="(item, index) in loginList" @click="loginControl(index)"  :key="index" :class="{'agent':index === curIndex}">{{item}}</span>
        </div>
        <div class="title-body">
          <div class="login-div" v-show="curIndex === 0">
            <div class="inputbox">
              <input
                type="text"
                placeholder="手机号码"
                v-model="login.phone"
              />
            </div>

            <div class="inputbox">
              <input type="password" placeholder="密码" v-model="login.password" />
            </div>
            <div class="loginbtn" @click="loginClick">登录</div>
          </div>
          <div class="register-div" v-show="curIndex === 1">
            <div class="inputbox">
              <input
                type="text"
                placeholder="用户昵称"
                v-model="register.nickName"
              />
            </div>
            <div class="inputbox">
              <input
                type="text"
                placeholder="手机号码"
                v-model="register.phone"
              />
            </div>
            <div class="inputbox">
              <input type="password" placeholder="密码" v-model="register.password" />
            </div>
            <div class="loginbtn" @click="registerClick">注册</div>
          </div>
        </div>
        <div class="message">
          <div class="forget">
            <span class="register" @click="curIndex=1" v-show="curIndex===0">立即注册</span>
            <!-- <span>忘记密码</span> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import Identify from '../../components/identify'
export default {
  name: "login",
  components: {
    Identify
  },
  data() {
    return {
      curIndex: 0,
      loginList: ['账号登录', '账号注册'],
      login: { // 登录所需参数
        phone: '',
        password: ''
      },
      register: { // 注册所需参数
        nickName: '',
        phone: '',
        password: ''
      }
    };
  },
  methods: {
    ...mapMutations(['saveUser']),
    /**
     * tab页切换
     */
    loginControl (index) {
      this.curIndex = index
    },
    loginClick() {
      if (!this.login.phone) {
        this.$message({
          message: '商品号为空，请正确输入',
          type: 'warning'
        });
        return
      }
      if (!this.login.password) {
        this.$message({
          message: '密码为空，请正确输入',
          type: 'warning'
        });
        return
      }
      this.$api.auth.login(this.login).then(res => {
        if (res.code === 200) {
          this.saveUser(res.data)
          this.$router.push("/index");

        }
      })
    },
    registerClick() {
      if (!this.register.nickName) {
        this.$message({
          message: '用户昵称为空，请正确输入',
          type: 'warning'
        });
        return
      }
      if (!this.register.phone) {
        this.$message({
          message: '商品号为空，请正确输入',
          type: 'warning'
        });
        return
      }
      if (!this.register.password) {
        this.$message({
          message: '密码为空，请正确输入',
          type: 'warning'
        });
        return
      }
      this.$api.auth.register(this.register).then(res => {
        if (res.code === 200) {
          this.curIndex = 0
        }
      })
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  position: relative;
  .login-bg {
    width: 100%;
    height: 576px;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    position: absolute;
    top: 112px;
    img {
      width: 100%;
    }
  }
  .center {
    position: relative;
    height: 576px;
    .login-box {
      position: absolute;
      width: 410px;
      height: 510px;
      background-color: #fff;
      right: 0;
      top: 35px;
      padding: 0 30px;
      box-sizing: border-box;
      border: 1px solid #ff6600;
      .title {
        margin: 40px 0;
        display: flex;
        text-align: center;
        span {
          flex: 1;
          height: 25px;
          line-height: 25px;
          font-size: 20px;
          &:first-child{
            border-right: 2px solid #d7d7d7;
          }
        }
          .agent {
          color: #ff6600;
        }
      }
      .title-body{
        .inputbox {
        width: 348px;
        height: 50px;
        border: 2px solid #eee;
        margin-top: 20px;
        input {
          width: 100%;
          height: 100%;
          border: none;
          text-indent: 20px;
          line-height: 50px;
        }
      }
      .identify-box{
        margin-top: 20px;
        display: flex;
        flex-flow: row nowrap;
        align-items: center;
        justify-content: space-between;
        input {
          width: 150px;
          height: 40px;
          border: none;
          text-indent: 20px;
          line-height: 50px;
          border: 2px solid #eee;
        }
      }
      }
    }
  }
}
.loginbtn {
  width: 348px;
  height: 50px;
  background-color: #ff6600;
  margin-top: 30px;
  text-align: center;
  line-height: 50px;
  font-size: 16px;
  color: #fff;
  cursor: pointer;
}
.message {
  display: flex;
  margin-top: 10px;
}
.number {
  flex: 1;
  color: #ff6600;
}
.forget {
  flex: 1;
  text-align: right;
}
.forget span {
  padding: 0 10px;
  color: #d7d7d7;
}
.forget span:nth-child(2) {
  padding-right: 0;
  border-left: 1px solid #eee;
}
.register {
  cursor: pointer;
}
.register:hover {
  color: #ff6600;
}

.header {
  height: 112px;
  background-color: #fff;
}
.header > a {
  display: inline-block;
}
img {
  margin-top: 4px;
}

.footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 80px;
}
.footer .info {
  height: 20px;
  line-height: 20px;
  font-size: 14px;
  margin: 15px 0;
}
.footer .info a {
  color: #333;
  padding: 0 15px;
  border-right: 1px solid #666;
}
.footer .copyright {
  height: 20px;
  line-height: 20px;
  font-size: 14px;
}
</style>
